// router/index.js （文献[3][6]）
import { createRouter, createWebHashHistory } from 'vue-router'
import { useAuthStore } from '@/stores'

// 必须导出router实例（文献[7]）
const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        { path: '/login', component: () => import('@/views/Login.vue') },
        { path: '/oauth/callback', component: () => import('@/components/oauth/OauthCallback.vue') },
        { 
            path: '/',
            component: () => import('@/views/Layout.vue'),
            meta: { requiresAuth: true },
            children: [
                {
                    path: '/system',
                    name: 'system',
                    children: [
                        {
                            path: '/system/menu', 
                            name: 'system_menu',
                            children: [
                                {
                                    path: '/system/menu/list',
                                    name: 'menu_list',
                                    component: () => import('@/views/menu/MenuList.vue'),
                                    meta: {
                                        title: '菜单管理',
                                        icon: 'Menu',
                                        keepAlive: true,
                                    }
                                },
                            ]
                        },
                        {
                            path: '/system/profile', 
                            name: 'system_profile',
                            component: () => import('@/views/user/Profile.vue'),
                            meta: {
                                    title: '个人信息',
                                    icon: 'Profile',
                                    keepAlive: true,
                                }


                        },
                        {
                            path: '/system/role', 
                            name: 'role_list',
                            component: () => import('@/views/user/RoleList.vue'),
                            meta: {
                                    title: '角色管理',
                                    icon: 'Role',
                                    keepAlive: true,
                                }
                        },
                        {
                            path: '/system/user', 
                            name: 'user_list',
                            component: () => import('@/views/user/UserList.vue'),
                            meta: {
                                    title: '用户管理',
                                    icon: 'User',
                                    keepAlive: true,
                                }
                        },
                    ]
                },
                {
                    path: '/order',
                    name: 'order',
                    children: [
                        {
                            path: '/order/list', 
                            name: 'order_list',
                            children: [
                                {
                                    path: '/order/list/all',
                                    name: 'order_all',
                                    component: () => import('@/views/order/OrderList.vue')
                                },
                            ]
                         },
                    ]
                },
                {
                    path: '/product',
                    name: 'product',
                    children: [
                        {
                            path: '/product/list', 
                            name: 'product_list',
                            component: () => import('@/views/product/ProductList.vue') ,
                            children: [
                                
                            ]
                         },
                    ]
                },
                {
                    path: '/stock',
                    name: 'stock',
                    children: [
                        {
                            path: '/stock/chart',
                            name: 'stock_chart',
                            component: () => import('@/views/stock/StockChart.vue'),
                            meta: {
                                title: '可转债指数',
                                icon: 'TrendCharts',
                                keepAlive: true,
                            }
                        },
                        {
                            path: '/stock/strategy',
                            name: 'Strategy',
                            component: () => import('@/views/stock/StrategyAnalysis.vue'),
                            meta: {
                                title: '策略分析',
                                icon: 'Strategy',
                                keepAlive: true,
                            }
                        }
                    ]
                },
                {
                    path:'/system/calendar',
                    name:'calendar',
                    component: () => import('@/views/system/Calendar.vue'),
                    meta: {
                        title: '日历',
                        icon: 'Calendar',
                        keepAlive: true,
                    }
                },
                {
                    path: '/system/bettercalendar',
                    name: 'bettercalendar',
                    component: () => import('@/views/system/BetterCalendar.vue'),
                    meta: {title: '日历-增强版', icon: 'Calendar' }
                }
            ]
        },
        { 
        path: '/register', 
        component: () => import('@/views/user/Register.vue')
        },
        { 
            path: '/redirect',
            component: () => import('@/views/Layout.vue'),
            hidden: true,
            children: [
                {
                    path: '/redirect/:path(.*)',
                    component: () => import('@/views/Redirect.vue')
                }
            ]
        },
        { path: '/:catchAll(.*)', component: () => import('@/views/NotFound.vue') }
    ]
})

router.beforeEach((to, from, next) => {
    const store = useAuthStore()
    
    // OAuth 回调路由特殊处理
    if (to.path === '/oauth/callback') {
        if (to.query.error) {
            next({
                path: '/login',
                query: { error: to.query.error }
            })
            return
        }
        next()
        return
    }
    
    if (to.path === '/login' && store.isAuthenticated) {
        next('/')
        return
    }
    
    if (to.meta.requiresAuth && !store.isAuthenticated) {
        next('/login')
        return
    }
    
    // 设置页面标题
    document.title = to.meta.title ? `IAMU -${to.meta.title}` : 'GodBless管理系统'
    
    next()
})

export default router


